﻿using Microsoft.AspNetCore.Mvc;
using static HcyWebAPI.Controllers.Params.AccountParams;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace HcyWebAPI.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class EmployeeController : ControllerBase
    {
        private readonly MyDbContext _context = new MyDbContext();

        /// <summary>
        /// 查询所有用户 根据条件
        /// </summary>
        /// <param name="params"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("list")]
        public Models.ResultMsg GetAccountList(AccountListParams @params)
        {
            Models.ResultMsg resultMsg = new Models.ResultMsg();
            Models.ResultDataInfo resultDataInfo = new Models.ResultDataInfo();
            Func<Models.Account, bool> whereLam = (p => true);
            if (@params.username != null && @params.username != "")
            {
                whereLam = p => p.Username.Contains(@params.username);
            }
            if (@params.deptId != 0)
            {
                whereLam = (p => p.DeptId == @params.deptId);
            }

            var temp = _context.Account.Where(whereLam)
                                     .OrderBy(p => p.Id)
                                     .Skip(@params.pageSize * (@params.pageIndex - 1))  //跳过多少
                                     .Take(@params.pageSize)                            //取多少
                                     .ToList();
            var count = _context.Account.Count(whereLam);
            //最后转为集合

            resultDataInfo.data = temp;
            resultDataInfo.total = count;
            resultMsg.data = resultDataInfo;
            return resultMsg;
        }

        //// GET: api/<EmployeeController>
        //[HttpGet]
        //public IEnumerable<string> Get()
        //{
        //    return new string[] { "value1", "value2" };
        //}

        //// GET api/<EmployeeController>/5
        //[HttpGet("{id}")]
        //public string Get(int id)
        //{
        //    return "value";
        //}

        //// POST api/<EmployeeController>
        //[HttpPost]
        //public void Post([FromBody] string value)
        //{
        //}

        //// PUT api/<EmployeeController>/5
        //[HttpPut("{id}")]
        //public void Put(int id, [FromBody] string value)
        //{
        //}

        //// DELETE api/<EmployeeController>/5
        //[HttpDelete("{id}")]
        //public void Delete(int id)
        //{
        //}
    }
}
